﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MapManager2 : MonoBehaviour
{   
    public GameObject outWall;
    public GameObject[] floorArray;
    public GameObject[] wallArray;
	public GameObject[] foodArray;
    public GameObject[] enemyArray;
    public GameObject exitPre;

    //list 存放位置
    public List<Vector2> positionList=new List<Vector2>();
    public int rows=10;
    public int cols=10;

    public Transform mapHolder;
    public int minCounWall=2;
    public int maxCounWall=8;

    private GameManager gameManager;

    private void Awake() {
        gameManager=this.GetComponent<GameManager>();
        InitMap();
    }
    

    // Start is called before the first frame update
    void Start()
    {   
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    //初始化地图
    private void InitMap(){
        //存放地板等
        mapHolder=new GameObject("Map").transform;
        //创建围墙和地板
        for(int x=0;x<cols;x++){
            for(int y=0;y<rows;y++){
                if(x==0||y==0 ||x==cols-1||y==rows-1){
                    GameObject go=GameObject.Instantiate(outWall,new Vector3(x,y,0),Quaternion.identity) as GameObject;
                    go.transform.SetParent(mapHolder);
                }else
                {
                    int index=Random.Range(0,floorArray.Length);
                    GameObject go=GameObject.Instantiate(floorArray[index],new Vector3(x,y,0),Quaternion.identity) as GameObject;
                    go.transform.SetParent(mapHolder);  
                }
            }
        }
        //创建障碍物
        positionList.Clear();
        //将障碍物的位置xy放入数组
        for(int x=2;x<cols-2;x++){
            for(int y=2;y<rows-2;y++){
                positionList.Add(new Vector2(x,y));
            }
        }

        //障碍物个数
        int wallConunt=Random.Range(minCounWall,maxCounWall+1);
        InstantiateItems(wallConunt,wallArray);
        //创建食物 2~level*2
        int foodCount=Random.Range(2,gameManager.level*2+1);
        InstantiateItems(foodCount,foodArray);
        //创建敌人
        int enemyCount=gameManager.level/2;
        InstantiateItems(enemyCount,enemyArray);
        //创建出口
        GameObject goExit=Instantiate(exitPre,new Vector2(cols-2,rows-2),Quaternion.identity)as GameObject;
        goExit.transform.SetParent(mapHolder);
    }
    //获取生成个数和数组，生成障碍物/食物/敌人等
    private void InstantiateItems(int count,GameObject[] perfabs){
        for(int i=0;i<count;i++){
            //随机取得位置
            Vector2 pos=RandomPos();
            //随机取得障碍物
            GameObject go1=RandomPrefab(perfabs);
            //实例化障碍物
            GameObject go2=Instantiate(go1,pos,Quaternion.identity) as GameObject;
            //父对象为mapHolder
            go2.transform.SetParent(mapHolder);
        }
    }
    
    //随机取得位置
    private Vector2 RandomPos(){
        int positionIndex=Random.Range(0,positionList.Count);
            Vector2 pos=positionList[positionIndex];
            positionList.RemoveAt(positionIndex);
            return pos;
    }
    //随机取得对象数组中的元素
    private GameObject RandomPrefab(GameObject[] perfabs){
        int index=Random.Range(0,perfabs.Length);
        return perfabs[index];
    }
}
